import 'dart:convert';

import 'package:get/get.dart';
import 'package:share_fit/app/controllers/base/base_controller.dart';
import 'package:share_fit/app/controllers/device/alarm/alarm_item.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/ute/uteped_config.dart';
import 'package:share_fit/app/ute/uteped_const.dart';
import 'package:share_fit/generated/locales.g.dart';

import 'alarm_state.dart';

class AlarmLogic extends BaseController {
  final AlarmState state = AlarmState();
  static String M_setAlarmList = "setAlarmList";
  static String M_getAlarmList = "getAlarmList";

  void loading(bool loading) {
    state.isLoading = loading;
    update(['update_alarm_datas']);
  }

  void addItem(AlarmItem item) {
    state.itemDatas.insert(state.itemDatas.length, item);
    update(['update_alarm_datas']);
  }

  void updateItem() {
    update(['update_alarm_datas']);
  }

  void updateAlarmRightText(String id, String value) {
    switch(id) {
      case AlarmState.alarmRepeatId:
        state.alarmRepeat = value;
        break;
      case AlarmState.alarmVibrateId:
        state.alarmVibrate = value;
        break;
      case AlarmState.alarmLabelId:
        state.alarmLabel = value;
        break;
    }
    update([id]);
  }

  String getAlarmRightText(String id) {
    switch(id) {
      case AlarmState.alarmRepeatId:
        return state.alarmRepeat;
      case AlarmState.alarmVibrateId:
        return state.alarmVibrate;
      case AlarmState.alarmLabelId:
        return state.alarmLabel;
    }
    return '';
  }

  void updateAlarmLater(bool value) {
    state.alarmAddLater = value;
    update(['update_alarm_later']);
  }

  void updateAlarmRepeat(String id, bool value) {
    switch(id) {
      case AlarmState.alarmRepeat1Id:
        state.alarmRepeat1 = value;
        break;
      case AlarmState.alarmRepeat2Id:
        state.alarmRepeat2 = value;
        break;
      case AlarmState.alarmRepeat3Id:
        state.alarmRepeat3 = value;
        break;
      case AlarmState.alarmRepeat4Id:
        state.alarmRepeat4 = value;
        break;
      case AlarmState.alarmRepeat5Id:
        state.alarmRepeat5 = value;
        break;
      case AlarmState.alarmRepeat6Id:
        state.alarmRepeat6 = value;
        break;
      case AlarmState.alarmRepeat7Id:
        state.alarmRepeat7 = value;
        break;
    }
    update([id]);
  }

  bool getAlarmRepeatState(String id) {
    bool result = false;
    switch(id) {
      case AlarmState.alarmRepeat1Id:
        result = state.alarmRepeat1;
        break;
      case AlarmState.alarmRepeat2Id:
        result = state.alarmRepeat2;
        break;
      case AlarmState.alarmRepeat3Id:
        result = state.alarmRepeat3;
        break;
      case AlarmState.alarmRepeat4Id:
        result = state.alarmRepeat4;
        break;
      case AlarmState.alarmRepeat5Id:
        result = state.alarmRepeat5;
        break;
      case AlarmState.alarmRepeat6Id:
        result = state.alarmRepeat6;
        break;
      case AlarmState.alarmRepeat7Id:
        result = state.alarmRepeat7;
        break;
    }
    return result;
  }

  void onDidPop(didPop) {
    if(!didPop) {
      var result = '';
      int count = 0;
      int isWorkDay = 0;
      int cycleList = state.WeekCycleONCE;
      int cycleListIOS = state.WeekCycleONCE;
      // state.cycleList.clear();
      if(state.alarmRepeat1) {
        result += '${LocaleKeys.alarm_week_monday.ts} ';
        cycleList += state.WeekCycleMONDAY;
        cycleListIOS += state.WeekCycleMONDAYIOS;
        count++;
        isWorkDay++;
      }
      if(state.alarmRepeat2) {
        result += '${LocaleKeys.alarm_week_tuesday.ts} ';
        cycleList += state.WeekCycleTUESDAY;
        cycleListIOS += state.WeekCycleTUESDAYIOS;
        count++;
        isWorkDay++;
      }
      if(state.alarmRepeat3) {
        result += '${LocaleKeys.alarm_week_wednesday.ts} ';
        cycleList += state.WeekCycleWEDNESDAY;
        cycleListIOS += state.WeekCycleWEDNESDAYIOS;
        count++;
        isWorkDay++;
      }
      if(state.alarmRepeat4) {
        result += '${LocaleKeys.alarm_week_thursday.ts} ';
        cycleList += state.WeekCycleTHURSDAY;
        cycleListIOS += state.WeekCycleTHURSDAYIOS;
        count++;
        isWorkDay++;
      }
      if(state.alarmRepeat5) {
        result += '${LocaleKeys.alarm_week_friday.ts} ';
        cycleList += state.WeekCycleFRIDAY;
        cycleListIOS += state.WeekCycleFRIDAYIOS;
        count++;
        isWorkDay++;
      }
      if(state.alarmRepeat6) {
        result += '${LocaleKeys.alarm_week_saturday.ts} ';
        cycleList += state.WeekCycleSATURDAY;
        cycleListIOS += state.WeekCycleSATURDAYIOS;
        count++;
        isWorkDay--;
      }
      if(state.alarmRepeat7) {
        result += '${LocaleKeys.alarm_week_sunday.ts} ';
        cycleList += state.WeekCycleSUNDAY;
        cycleListIOS += state.WeekCycleSUNDAYIOS;
        count++;
        isWorkDay--;
      }

      state.cycleListInt = cycleList;
      state.cycleListIntIOS = cycleListIOS;

      if(isWorkDay==5) {
        result = LocaleKeys.alarm_workday.ts;
        state.cycleListInt = state.WeekCycleWORKDAY;
        // state.cycleList.insert(0, state.WeekCycleWORKDAY);
      }
      if(count==7) {
        result = LocaleKeys.alarm_everyday.ts;
        state.cycleListInt = state.WeekCycleEVERYDAY;
        // state.cycleList.insert(0, state.WeekCycleEVERYDAY);
      }
      if(result.isEmpty) {
        result = LocaleKeys.alarm_never.ts;
        state.cycleListInt = state.WeekCycleONCE;
        // state.cycleList.insert(0, state.WeekCycleONCE);
      }

      // if(state.cycleList.isEmpty) {
      //   state.cycleListInt = cycleList;
      //   // state.cycleList.insert(0, cycleList);
      // }
      state.alarmRepeat = result;
      Get.back(result: result);
    }
  }

  Future<void> setAlarmList({required Null Function(dynamic _) back}) async {
    List<Map> lists = [];
    state.itemDatas.forEach((element) {
      lists.insert(0, element.toMap());
    });
    Map<String, List<Object>> params = {'list': lists};
    var result = await dRemoteReqAsync(M_setAlarmList, params);
    back(result);
  }

  /// 保存的时候周期字段不含周一到周五任意一天，实际没有保存，多个闹钟，只保存成功一个
  Future<void> getAlarmList() async {
    var result = await dRemoteReqAsync(M_getAlarmList, {});
    print('getAlarmList $result');
    //[{getName: Alarm, getCycleList: null, getIndex: 1, getEnable: true, getMinute: 6, getCycle: 98, getHour: 15}]
    loading(false);
    for (var element in result) {
      Map<dynamic, dynamic> mapData = json.decode(jsonEncode(element));
      int cycleList = mapData['cycle'];
      AlarmItem item = AlarmItem();
      item.name = mapData['name'] ?? '';
      item.hour = mapData['hour'];
      item.minute = mapData['minute'];
      item.index = mapData['index'];
      item.enable = mapData['enable'];
      item.cycleList.insert(0, cycleList);
      addItem(item);
    }
  }

  @override
  String getRemotePath() {
    return UTEPedConst.pathDeviceOptions;
  }

  @override
  String getStoragePath() {
    return UTEPedConst.pathDevice;
  }

  @override
  Future<void> initState() async {
    getAlarmList();
    state.is24HourFormat = read(UTEPedConst.keyTimeFormat, defaultValue: false);
  }

  @override
  void onRemoteEvent(event, method) {
    // TODO: implement onRemoteEvent
  }
}
